import { Component, OnInit, Output, ViewChild, EventEmitter, ElementRef } from '@angular/core';
import { Router } from '@angular/router';
import { Service } from '../../server/server';
import { LoginService } from '../services/login.service';
import { HeaderService } from '../services/header.service';
import { } from 'rxjs';
//import { setInterval } from 'timers';
declare var $: any;

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
})
export class LoginComponent implements OnInit {

  verifyImage: any;
  verifyCodeHash: string;
  errorCode: string = '';
  errorMessage: string = '';
  loading: boolean = false;
  userId: string;

  constructor(private server: Service, private service: LoginService, private router: Router, private headerService: HeaderService) { }

  ngOnInit() {
    this.getVerify();

    if (this.keyupcap) { $('.tooltip-show').tooltip('show'); }
    $(document).keydown((e) => {
      if (e.which == 20) {
        e.preventDefault();
        this.keyupcap = !this.keyupcap;
        if (this.keyupcap) {
          $('.tooltip-show').tooltip('show');
        } else {
          $('.tooltip-show').tooltip('hide');
        }
      }
    });

  }

  //键盘大小写
  public keyupcap: any = false;
  public loginindex: any;

  public loginTitledata: string = '登录';
  getVerify(): void {
    this.service.getVerify().then((data: any) => {
      this.verifyImage = data.imageParam;
      this.verifyCodeHash = data.verifyCode;
      $("#imgVerify").attr("src", data.imageParam);

    }).catch(err => {
      console.error('An error occurred', err);
    });
  }
  //登录文字显示

  checkSum(val): void {
    let param = {
      userName: val.loginuserName,
      password: val.loginpassword,
      verifyCode: val.loginvalidationCode.toUpperCase(),
      verifyCodeHash: this.verifyCodeHash
    };
    this.loading = true;
    this.loginTitledata = 'Loading...';
    this.service.checkSum(param).then((data: any) => {
      let code: string = data.result_code;
      let backUrl: string = data.back_url;

      if (code === '0000') {
        this.loginTitledata = 'Loading...';
        this.loading = true;
        //console.log(data)
        //存权限
        this.server.right = this.service.processRights(data.result);
        this.server.setRight(this.server.right);
        //存操作用户id
        this.server.createUserId = data.userId;
        this.server.setuser(data.userId);
        //存昵称
        this.server.username = data.displayName;
        this.server.setuserName(data.displayName);
        //存用户名
        this.server.realName = data.realName;
        this.server.setRealName(data.realName);
        // //存头像portrait
        // this.server.portrait = data.portrait;
        // this.server.setuserPortrait(data.portrait);
        // //存门户信息
        // this.server.websites = data.websites;
        // this.server.setwebsites(data.websites);
        //存机构
        // this.server.institution = data.institutionId;
        // this.server.setInstitution(data.institutionId);
        if (!backUrl) {
          this.router.navigateByUrl('login');
        } else {
          //this.router.navigateByUrl(backUrl);
          this.loginindex = setTimeout(() => {
            this.headerService.index();
            //代办连接与打开窗口
            // this.websocketService.sendMsg(this.server.getuserName());
            // this.websocketService.hornClick.emit("open-model");
            clearInterval(this.server.intervalId);
            this.server.openTime(this.server.exitMaxTime);
            }, 300);

        }
        //定时清除session
        // if(data.defaultTime == 1){
        //   setTimeout(()=>{
        //     sessionStorage.clear();
        //     this.router.navigateByUrl('login');
        //     console.log("清除")
        //   },10000)
        // }



        return;
      } else {
        this.loginTitledata = '登录';
        this.loading = false;
      }
      //2222,1111,4444,9999分别代表username,password,loginNull,登录验证
      this.errorCode = code;
      this.errorMessage = data.message;
      this.getVerify();

      this.loading = false;
    }).catch(err => {
      console.error('An error occurred', err);
    });
  }

 

}

